(function() {

    var FoodTable = function(label, color) {
        this.initialize(label, color);
    };


    var p = FoodTable.prototype = new createjs.Container(); // inherit from Container

    this.bmp;
    p.background;
    p.hitScale = 0.1;

    p.Container_initialize = p.initialize;
    p.initialize = function(image) {
        this.Container_initialize();

        this.bmp = new createjs.Bitmap(queue.getResult(image));
        
//        var shadow = new createjs.Shadow("#333", 10, 10, 500);
//        this.bmp.shadow = shadow;
        stage.addChild(this.bmp);

        this.mouseChildren = false;
    };

    p.hitScale = function() {
        this.bmp.scaleX += p.hitScale;
        this.bmp.scaleY += p.hitScale;
    };

    p.unhitScale = function() {
        this.bmp.scaleX -= p.hitScale;
        this.bmp.scaleY -= p.hitScale;
    };

    p.addClickListener = function(fn) {
        this.bmp.addEventListener("click", fn);
    };

    p.size = function(windowWidth, windowHeight) {
        var bounds = this.bmp.getBounds();
        var currentWidth = bounds.width;
        var currentHeight = bounds.height;

        // Derive a scale. Use the smaller of the two to "fit" the image. Change to Math.max to make it "fill" the window
        var scale = Math.min(windowWidth / currentWidth, windowHeight / currentHeight);
        this.bmp.scaleX = this.bmp.scaleY = scale;
    };

    p.appearSpin = function(canvasWidth, canvasHeight) {
        var bounds = this.bmp.getBounds();
        //move it's rotation center at the center of the form
        this.bmp.regX = bounds.width * 0.5;
        this.bmp.regY = bounds.height * 0.5;
        //move the form above the screen
        this.bmp.x = canvasWidth * 0.5;
        this.bmp.y = -200;
        createjs.Tween.get(this.bmp).to({alpha: 1, y: 450, rotation: 720}, 2000, createjs.Ease.cubicOut).call(this.tweenComplete);
    };

    p.disappearScale = function() {
        p.alpha = 0;
        createjs.Tween.get(this.bmp).to({scaleX:0,scaleY:0,alpha:1}, 2000, createjs.Ease.backIn);
    };

    p.appearScale = function(canvasWidth) {
        p.alpha = 0;
        this.bmp.x = (canvasWidth - (this.bmp.getBounds().width * this.bmp.scaleX)) * 0.5;
        this.bmp.y = 300;
        createjs.Tween.get(this.bmp).to({scaleX:0.5,scaleY:0.5,alpha:1}, 2000, createjs.Ease.backIn);
    };

    p.handleClick = function(event) {
        var target = event.target;
        alert("You clicked on a button: " + target.label);
    };

    p.handleTick = function(event) {
        p.alpha = Math.cos(p.count++ * 0.1) * 0.4 + 0.6;
    };

    window.FoodTable = FoodTable;
}());